package fun.sanguo.caches;

import fun.sanguo.ApplicationContextProvider;
import fun.sanguo.model.dto.PageInfo;
import fun.sanguo.model.dto.PageQuery;
import fun.sanguo.model.dto.sys.SysUserParam;
import fun.sanguo.model.entity.sys.SysUserEntity;
import fun.sanguo.service.sys.SysUserService;
import org.apache.commons.lang3.StringUtils;
import org.apache.mina.util.ExpiringMap;

import java.util.concurrent.ConcurrentHashMap;

/**
 * 用户缓存数据
 *
 * @author ZYP
 * @date 2023-04-27
 */
public class UserCache {

    /**
     * 验证码缓存，设置90秒过期自动删除
     */
    private static ExpiringMap<String, String> captchaMap = new ExpiringMap<>(90, 1);

    /**
     * 短信验证码缓存，设置90秒过期自动删除
     */
    private static ExpiringMap<String, String> smsCaptchaMap = new ExpiringMap<>(90, 1);

    static {
        captchaMap.getExpirer().startExpiring();
        smsCaptchaMap.getExpirer().startExpiring();
    }

    /**
     * 系统用户信息缓存，id-entity
     */
    private static ConcurrentHashMap<String, SysUserEntity> userMap = new ConcurrentHashMap<>();

    /**
     * 将登陆序列号与验证码放入缓存
     *
     * @param loginSeq
     * @param captcha
     */
    public static void putCaptcha(String loginSeq, String captcha) {
        captchaMap.put(loginSeq, captcha);
    }

    /**
     * 获取验证码
     *
     * @param loginSeq
     * @return
     */
    public static String getCaptcha(String loginSeq) {
        return captchaMap.get(loginSeq);
    }

    /**
     * 将下发手机号与短信验证码放入缓存
     *
     * @param mobile
     * @param smsCaptcha
     */
    public static void putSmsCaptcha(String mobile, String smsCaptcha) {
        smsCaptchaMap.put(mobile, smsCaptcha);
    }

    /**
     * 获取短信验证码
     *
     * @param mobile
     * @return
     */
    public static String getSmsCaptcha(String mobile) {
        return smsCaptchaMap.get(mobile);
    }

    /**
     * 根据用户id获取用户名
     *
     * @param id
     * @return
     */
    public static String getUsernameById(String id) {
        SysUserEntity sysUserEntity = userMap.get(id);
        // 获取不到则更新
        if (sysUserEntity == null) {
            refreshUserCache();
            sysUserEntity = userMap.get(id);
        }
        // 返回 nickname > username > "佚名"
        if (sysUserEntity == null) {
            return "佚名";
        } else if (StringUtils.isNotEmpty(sysUserEntity.getNickname())) {
            return sysUserEntity.getNickname();
        } else if (StringUtils.isNotEmpty(sysUserEntity.getUsername())) {
            return sysUserEntity.getUsername();
        } else {
            return "佚名";
        }
    }

    private static SysUserService sysUserService = ApplicationContextProvider.getBean(SysUserService.class);

    /**
     * 更新系统用户缓存
     */
    public static synchronized void refreshUserCache() {
        // 查询条件
        PageQuery<SysUserParam> pageQuery = new PageQuery<>();
        pageQuery.setParam(new SysUserParam());
        pageQuery.setPageNum(0);
        pageQuery.setPageSize(0);
        // 查询所有系统用户
        try {
            PageInfo<SysUserEntity> sysUserEntityPageInfo = sysUserService.getPage(pageQuery);
            // 查询结果不空则替换全局变量
            if (sysUserEntityPageInfo != null && sysUserEntityPageInfo.getList() != null) {
                userMap.clear();
                sysUserEntityPageInfo.getList().forEach(entity -> {
                    userMap.put(entity.getId(), entity);
                });
            }
        } catch (Exception e) {
            // ignore error
        }
    }
}